package com.duobaoyu.dby.supply.biz.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONValidator;
import com.alibaba.fastjson.TypeReference;
import com.duobaoyu.dby.channel.adapter.enums.biz.refund.AfterSaleTypeEnum;
import com.duobaoyu.dby.supply.common.biz.vo.order.SaleAttributeVO;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName BaseEnumReferenceMapper
 * @createTime 19:00:00 2023/10/08
 */
@Component
public class CustomReferenceMapper {

    public AfterSaleTypeEnum toAfterSaleTypeEnum(Integer val) {
        return AfterSaleTypeEnum.getAfterSaleTypeEnum(val);
    }

    public Integer toAfterSaleTypeValue(AfterSaleTypeEnum afterSaleTypeEnum) {
        return afterSaleTypeEnum.getValue();
    }

    public LocalDateTime toLocalDateTime(Long milliseconds) {
        if(Objects.isNull(milliseconds)) {
            return null;
        }
        return Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
    }

    public Long toMillisecondTimestamp(LocalDateTime localDateTime){
        if(Objects.isNull(localDateTime)) {
            return null;
        }
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public List<SaleAttributeVO> toSaleAttributeList(String saleAttributes) {
        if (JSONValidator.from(saleAttributes).validate()) {
            return JSON.parseObject(saleAttributes, new TypeReference<List<SaleAttributeVO>>(){});
        }
        return Collections.emptyList();
    }
}
